#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;
use users;
 
binmode STDOUT, ":utf8";
#use utf8;
require "doa_subs.pl";
 
use JSON;
use CGI;
use lib '.';
use users;

my ($username,$level) = &validate;
my ($realm) = get_realm();
my $realmdesc=get_realm_desc($realm);
my $q = new CGI;
print $q->header;
print $q->start_html('List of Alliances');
print $q->h2({ -align => "CENTER"},"List of Alliances ($realmdesc)");




############################## Main Prog ##############################

my $alliance_id={};
my $alliance_name={};
my $alliance_count=loadAlliance($alliance_id,$alliance_name);
my @dataout;

print $q->p({ -align => "CENTER"}, "We have $alliance_count known alliances in ($realmdesc)");
print $q->a( {-href=>"menu.cgi"}, "Main Menu");

foreach my $id ( keys %{$alliance_id})
{
	my $outstring=sprintf("%04d", $alliance_id->{$id}{'rank'});
	my $alliname=$alliance_id->{$id}{'name'};
	my $state=$alliance_id->{$id}{'state'}||" ";
	my $lord=$alliance_id->{$id}{'lord'}||" ";
	my $members=$alliance_id->{$id}{'members'}||" ";
	$outstring=$q->td($outstring).$q->td($q->a( {-href=>"/cgi-bin/findalliance2.pl?search=$alliname"}, $alliname));
	$outstring=$outstring.$q->td($lord).$q->td($members).$q->td($state);
	push (@dataout,$outstring);
}
@dataout=sort(@dataout);
print $q->table({-border=>undef,-width=>'25%', -align=>'center'},
		$q->Tr({ -align => "CENTER", -valign => "TOP" },$q->th(['Rank','Alliance Name','Lord','Number of Members','Friend Status'])),
        $q->Tr({ -align => "CENTER", -valign => "TOP" },\@dataout)
       );

print $q->br();
print $q->a( {-href=>"menu.cgi"}, "Main Menu");
print $q->end_html;



